package com.kylin.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kylin.R;
import com.kylin.eduservice.entity.EduTeacher;
import com.kylin.eduservice.entity.vo.TeacherQuery;
import com.kylin.eduservice.service.EduTeacherService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author kylin
 * @since 2020-07-13
 */
@RestController
@RequestMapping("/eduservice/teacher")
@CrossOrigin//解决跨域
public class EduTeacherController {


    //service注入
    @Autowired
    EduTeacherService teacherService;


    //查询讲师表所有数据
    @GetMapping("/findAll")
    @ApiOperation("查询所有讲师")
    public R findAll() {

        //调用service的方法实现查询所有的操作
        List<EduTeacher> list = teacherService.list(null);
/*        try {
            int i = 10 /0;
        }catch (Exception e)
        {
            throw new GuliException(20001,"出现自定义异常");
        }*/
        return R.ok().data("items", list);//链式编程 return r
    }

    //逻辑删除讲师
    @DeleteMapping("/{id}")
    @ApiOperation("根据ID删除讲师")
    public R removeTeacher(@PathVariable("id") String id) {

        boolean flag = teacherService.removeById(id);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    //分页查询讲师
    @GetMapping("/pageTeacher/{current}/{limit}")
    @ApiOperation("分页查询讲师")
    public R pageTeacher(@PathVariable("current") long current, @PathVariable("limit") long limit) {

        //创建page对象
        Page<EduTeacher> pageTeacher = new Page<>(current, limit);
        //调用方法的时候，底层封装，把分页的所有数据封装到page对象中
        teacherService.page(pageTeacher, null);
        //获取总记录数
        long total = pageTeacher.getTotal();
        //获取数据list集合
        List<EduTeacher> records = pageTeacher.getRecords();

        return R.ok().data("total", total).data("rows", records);
    }

    //条件查询
    @PostMapping("/pageTeacherCondition/{current}/{limit}")
    @ApiOperation("条件查询讲师")
    public R pageTeacherCondition(@PathVariable("current") long current,
                                  @PathVariable("limit") long limit,
                                  @RequestBody(required = false) TeacherQuery teacherQuery) {

        //创建page对象
        Page<EduTeacher> pageTeacher = new Page<>(current, limit);

        //构建条件
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        //多条件组合查询，类似mybatis中的动态sql
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        //判断条件值是否为空，如果不为空拼接条件,StringUtils为spring中的工具类
        if (!StringUtils.isEmpty(name)) {
            //构建条件 %值%' 第一个值为字段名，第二个为值
            wrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(level)) {
            //等于
            wrapper.eq("level", level);
        }
        if (!StringUtils.isEmpty(begin)) {
            //ge大于等于 >=
            wrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            //le小于等于 <=
            wrapper.le("gmt_modified", end);
        }
        //排序
        wrapper.orderByDesc("gmt_create");
        //调用方法的时候，底层封装，把分页的所有数据封装到page对象中
        teacherService.page(pageTeacher, wrapper);
        //获取总记录数
        long total = pageTeacher.getTotal();
        //获取数据list集合
        List<EduTeacher> records = pageTeacher.getRecords();

        return R.ok().data("total", total).data("rows", records);
    }


    //添加讲师方法
    @PostMapping("/addTeacher")
    @ApiOperation("添加讲师")
    public R addTeacher(@RequestBody EduTeacher eduTeacher) {
        //添加对象
        boolean flag = teacherService.save(eduTeacher);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    //根据讲师ID进行查询
    @GetMapping("getTeacher/{id}")
    @ApiOperation("根据ID进行查询讲师")
    public R getTeacher(@PathVariable("id") String id){
        EduTeacher eduTeacher = teacherService.getById(id);
        return R.ok().data("teacher",eduTeacher);
    }

    //根据讲师ID修改
    @PostMapping("/updateTeacher")
    @ApiOperation("修改讲师")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean flag = teacherService.updateById(eduTeacher);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }
}

